/**
 * 线路图层(单线)类
 */
Subwaymap.LineLayer = org.gridsofts.util.Class.define({
    name: "Subwaymap.LineLayer",

    /**
     * 构造函数
     */
    construct: function (container, line) {
        if (container == null || container == undefined || line == null || line == undefined) {
            throw new Error("Object construct parameter is empty, unable to initialize instance of LineLayer.");
        }

        // 处理参数
        this.container = container;
        this.line = line;

        this.layer = new Kinetic.Layer({
            id: "linelayer_" + line.routeMapRDVID,
            line: line
        });
        this.container.add(this.layer);

        this.repaint();
    },

    methods: {
        /**
         * 根据路网图数据，重绘路网线路
         */
        repaint: function () {
            this.layer.clear();

            //绘制线路
            this.drawBranch(this.line);
        },

        drawBranch: function (line) {
            var currentLine = line  //主线路

            //绘画线路名称  例如  XX 号线
            this.actualDrawLineName(currentLine.routePath, currentLine.displayName, currentLine.routeColor);
            // 拼接用于画线的路径
            this.actualDrawPath(currentLine.routePath, currentLine.routeMapRDVID, currentLine.routeColor, currentLine.stopsOnRoute)
        },

        /**
         * 调用绘图API，在画布上绘制图元
         */
        actualDrawPath: function (branchPathData, name, strokeColor, list_stopsOnRoute) {
            // 绘画路线
            var branchPathData = branchPathData.split('L').filter(a => a != "");
            for (var i = 0; i < branchPathData.length;) {
                if (branchPathData[i + 1] == undefined)
                    break;

                var path = new Kinetic.Path({
                    name: name,
                    lineCap: "round",
                    stroke: "#" + strokeColor,
                    strokeWidth: 14,
                    opacity: 1
                });

                var index = 0;
                if (branchPathData[i + 1].indexOf("M") == -1 && branchPathData[i + 1].indexOf("Q") == -1) {
                    var stopPostion = branchPathData[i + 1].trim().split(' ');
                    index = list_stopsOnRoute.findIndex(a => a.stopX == stopPostion[0] && a.stopY == stopPostion[1]);

                }
                //存在直角场景，第三个才匹配
                if (index == -1) {
                    path.data(("L" + branchPathData[i] + "L" + branchPathData[i + 1] + "L" + branchPathData[i + 2]).trim());
                    i = i + 2;
                } else {
                    path.data(("L" + branchPathData[i] + "L" + branchPathData[i + 1]).trim());
                    i = i + 1;
                }



                this.layer.add(path);

                path.on('click', function (evt) {
                    const currentRoute = evt.target.parent.attrs.line;
                    const displayName = currentRoute.displayName;
                    const list_stopsOnRoute = currentRoute.stopsOnRoute;
                    const routeMapRDVID = currentRoute.routeMapRDVID;

                    let stopX = evt.target.dataArray[0].points[0];
                    let stopY = evt.target.dataArray[0].points[1];
                    let stopList = evt.target.attrs.data.split('L').filter(a => a != "");
                    if (evt.target.attrs.data.indexOf("M") > -1 && stopList[0].indexOf("M") > -1) {
                        stopX = evt.target.dataArray[1].points[0];
                        stopY = evt.target.dataArray[1].points[1];
                    }

                    let sub_stopsOnRoute = list_stopsOnRoute.filter(a => a.seq >= list_stopsOnRoute.find(a => a.stopX == stopX && a.stopY == stopY).seq).splice(0, 3);
                    console.log(sub_stopsOnRoute)
                });
            }
        },

        actualDrawLineName: function (branchPathData, name, strokeColor) {
            // 绘画 XX 号线  文字描述
            // 创建  Group，将 多个 属性放入 Group 中
            var group = new Kinetic.Group({
                draggable: true
            });
            if (branchPathData?.split('L')?.length < 2) {
                return false;
            }

            var firstStationPostion = branchPathData?.split('L')[1].trim(' ').split(' ');
            var lineText = new Kinetic.Text({
                x: parseInt(firstStationPostion[0]) - 200,
                y: parseInt(firstStationPostion[1]) + 60,
                text: name,
                align: 'center',
                fontSize: 65,
                fontFamily: 'Arial',
                fill: "#" + strokeColor,
            });

            group.add(lineText);
            this.layer.add(group);
        }
    }
});



